package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Event9slr;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("Event9slrController")
public class Event9slrController extends CmiEmaController {

	@RequestMapping("/listEvent9slrs")
	public ModelAndView listEvent9slrs(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = event9slrPaginationService.updatePagination(request, pagination);
		Set<Event9slr> event9slrs = event9slrPaginationService.getPaginationEvent9slrs(pagination);

		mav.addObject("event9slrs", event9slrs);
		mav.addObject("entityName", "event9slr");
		mav.addObject("pagination", pagination);
		mav.setViewName("event9slr/listEvent9slrs");
		return mav;
	}

	public String listEvent9slrs() {
		return "redirect:/listEvent9slrs";
	}

	@RequestMapping("/viewEvent9slr")
	public ModelAndView viewEvent9slr(@RequestParam Integer event9slrId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("event9slr", event9slrDAO.findEvent9slrByPrimaryKey(event9slrId));
		mav.setViewName("event9slr/viewEvent9slr");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteEvent9slrs")
	public ModelAndView confirmDeleteEvent9slrs(
			@RequestParam(value = "deleteEvent9slrsIds", required = true) Set<Integer> deleteEvent9slrsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Event9slr> deleteEvent9slrs = event9slrDAO.findEvent9slrsByIds(deleteEvent9slrsIds);
		Set<Event9slr> referencedEvent9slrs = event9slrService.loadReferenceEvent9slrs(deleteEvent9slrs);
		boolean hasReferenced;
		if (referencedEvent9slrs.size() == 0) {
			mav.addObject("deleteEntitys", deleteEvent9slrs);
			hasReferenced = false;
		} else {
			deleteEvent9slrs.removeAll(referencedEvent9slrs);
			mav.addObject("referencedEntitys", referencedEvent9slrs);
			mav.addObject("deleteEntitys", deleteEvent9slrs);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Event9slr");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listEvent9slrs");
		return mav;
	}

	@RequestMapping("/deleteEvent9slr")
	public String deleteEvent9slr(@RequestParam Integer event9slrId) {
		Event9slr event9slr = event9slrDAO.findEvent9slrByPrimaryKey(event9slrId);
		event9slrService.deleteEvent9slr(event9slr);
		return "forward:/listEvent9slrs";
	}

	@RequestMapping("/confirmDeleteEvent9slr")
	public ModelAndView confirmDeleteEvent9slr(@RequestParam Integer event9slrId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("event9slr", event9slrDAO.findEvent9slrByPrimaryKey(event9slrId));

		mav.setViewName("event9slr/deleteEvent9slr");

		return mav;
	}

	@RequestMapping("/deleteEvent9slrs")
	public String deleteEvent9slrs(
			@RequestParam(value = "deleteEvent9slrsId", required = true) Set<Integer> deleteEvent9slrsId) {
		event9slrService.deleteEvent9slrs(deleteEvent9slrsId);
		return "forward:/listEvent9slrs";
	}

	@RequestMapping("/editEvent9slr")
	public ModelAndView editEvent9slr(@RequestParam Integer event9slrId) {

		ModelAndView mav = new ModelAndView();

		Event9slr event9slr = event9slrDAO.findEvent9slrByPrimaryKey(event9slrId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("event9slr", event9slr);

		// 把所有的引用event9slr对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(event9slrService.loadEvent9slrObjectReferencedSet(event9slr));

		// 将编辑页面event9slr添加到mav中
		mav.setViewName("event9slr/editEvent9slr");
		return mav;
	}

	@RequestMapping("/newEvent9slr")
	public ModelAndView newEvent9slr() {
		ModelAndView mav = new ModelAndView();
		Event9slr event9slr = new Event9slr();

		mav.addObject("event9slr", event9slr);

		// 把所有的引用event9slr对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(event9slrService.loadEvent9slrObjectReferencedSet(event9slr));

		mav.addObject("newFlag", true);
		mav.setViewName("event9slr/editEvent9slr");

		return mav;
	}

	@RequestMapping("/saveEvent9slr")
	public ModelAndView saveEvent9slr(@ModelAttribute Event9slr event9slr) {

		ModelAndView mav = new ModelAndView();

		Event9slr existingEvent9slr = CmiHibernateProxyUtil
				.deproxy(event9slrDAO.findEvent9slrByPhysicalPrimaryKey(event9slr.getNumber()));

		// 新建记录
		if (event9slr.getId() == null) {
			if (event9slr.equals(existingEvent9slr)) {

				mav.addAllObjects(event9slrService.loadEvent9slrClassReferencedSet(event9slr));
				// mav = editContinue(mav, event9slr);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("event9slr/editEvent9slr");
			} else {
				event9slrService.saveEvent9slr(event9slr);
				mav.setViewName("forward:/listEvent9slrs");
			}
		}
		// 编辑记录
		else {
			Event9slr originEvent9slr = CmiHibernateProxyUtil
					.deproxy(event9slrDAO.findEvent9slrByPrimaryKey(event9slr.getId()));
			if (!event9slr.equals(originEvent9slr) && event9slr.equals(existingEvent9slr)) {
				mav.addAllObjects(event9slrService.loadEvent9slrClassReferencedSet(event9slr));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("event9slr/editEvent9slr");
			} else {
				event9slrService.saveEvent9slr(event9slr);
				mav.setViewName("forward:/listEvent9slrs");
			}
		}
		return mav;
	}

	@RequestMapping("/event9slrController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
